import {
  user
} from '@api'
export default {
  async fetchMenu ({
    commit
  }, {
    appId
  }) {
    let res = await user()._serverMenu(appId)
    let routers = await user()._serverRouter(appId)
    commit('SET_ROUTERS', routers.data)

    const menuList = []
    for (let i = 0; i < res.data.length; i++) {
      if (!res.data[i].children || !res.data[i].children.length) {
        menuList.push({
          path: res.data[i].path,
          icon: res.data[i].icon,
          title: res.data[i].title.split('（')[0]
        })
      } else {
        let obj = {
          icon: res.data[i].icon,
          title: res.data[i].title.split('（')[0],
          group: res.data[i].path,
          children: []
        }
        obj.children = res.data[i].children.map(item => {
          return {
            icon: item.icon,
            path: item.path,
            title: item.title.split('（')[0]
          }
        })

        menuList.push(obj)
      }
    }

    commit('SET_MENU', {
      main: menuList
    })
    return res
  }
}
